const multer = require('multer');
const path = require('path');

// 配置存储
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'public/uploads/audio');
    },
    filename: function (req, file, cb) {
        // 确保文件名以.wav结尾
        const fileName = `audio-${req.user?.id || 'guest'}-${Date.now()}-${Math.floor(Math.random() * 1000000000)}`;
        cb(null, `${fileName}.wav`);
    }
});

// 文件过滤器
const fileFilter = (req, file, cb) => {
    // 只接受WAV文件
    if (file.mimetype === 'audio/wav' || file.mimetype === 'audio/wave' || file.mimetype === 'audio/x-wav') {
        cb(null, true);
    } else {
        cb(new Error('Only WAV files are allowed'), false);
    }
};

// 创建multer实例
const upload = multer({
    storage: storage,
    fileFilter: fileFilter,
    limits: {
        fileSize: 10 * 1024 * 1024 // 限制文件大小为10MB
    }
});

module.exports = upload; 